#ifndef UTIL_H
#define UTIL_H

/**
 * Various utility functions.
 *
 * @author Dejan Lekic <dejan.lekic@kcl.ac.uk
 */

namespace profo {

/**
 * Sets a bit with ID = idx to 1.
 * NOTE: that the first bit in a given byte "dest" has idx 0!
 * Example: int num = 0; bitOn(7, num);
 * Will set the most significant bit to 1, ie.
 *       76543210 <-- bit idx
 * num = 10000000
 */
inline bool bitOn(unsigned char idx, unsigned char& dest) {
  if (idx > 7) {
    return false;
  } else {
    dest = dest | ( 1 << idx);
    return true;
  } // else
} // setBit() function

/**
 * Sets a bit with ID = idx to 0.
 * NOTE: that the first bit in a given byte "dest" has idx 0!
 * Example: int num = 80; bitOff(7, num);
 * Will set the most significant bit to 0, ie.
 *       76543210 <-- bit idx
 * num = 00000000
 */
inline bool bitOff(unsigned char idx, unsigned char& dest) {
  if (idx > 7) {
    return false;
  } else {
    dest = dest & (dest ^ (1 << idx));
    return true;
  } // else
} // bitOff() function

/**
 * Returns true if bit with ID = idx is set, false if not, or if idx is greater
 * than 7 (because we are operating on BYTEs only.
 */
inline bool getBit(unsigned char idx, const unsigned char& dest) {
  if (idx > 7) {
    return false;
  } else {
    return dest & (1 << idx);
  } // else
} // getBit() function

} // profo namespace

// $Id: util.h 41 2009-04-25 01:33:39Z dejan.lekic $

#endif // UTIL_H
